<?php
/**
 * renren oauth flow
 *
 * @author tom.wang<tom.wang.sz@qq.com>
 */
class RenrenOauth {
	/**
	 * get authorize url
	 *
	 * @return string : the url used for user to authorize
	 */
	public function getAuthorizeUrl() {
		$url = RenrenClient::getCfg('authorize_url') . '?response_type=code&client_id=' . RenrenClient::getCfg('oauth_consumer_token') . '&redirect_uri=' . urlencode(RenrenClient::getCfg('callback'));
		
		return $url;
	}
	
	/**
	 * get access token
	 *
	 * @param string $params : see specific method below
	 * @param string $type : the fetch type, normal, password
	 * @return string : access token
	 */
	public function getAccessToken($params, $type = 'normal') {
		$method = 'getAccessTokenBy' . ucfirst(strtolower($type));
		$result = call_user_func(array($this, $method), $params);
		$this->handleError($result);
		return $result;
	}
	
	/**
	 * get access token(normal way)
	 * 
	 * @author tom.wang<tom.wang.sz@qq.com>
	 * @param string $params['code'] : the authorized code
	 * @return string : access token 
	 */
	private function getAccessTokenByNormal($params) {
		$code = $params['code'];
		$url = RenrenClient::getCfg('access_token_url');
		$params = array(
			'redirect_uri' => RenrenClient::getCfg('callback'),
			'grant_type' => 'authorization_code',
			'code' => $code,
			'client_id' => RenrenClient::getCfg('oauth_consumer_token'),
			'client_secret' => RenrenClient::getCfg('oauth_consumer_token_secret'),
		);
		$ret = RenrenUtil::http($url, 'POST', $params);
		$ret = json_decode($ret, true);
		
		return $ret;
	}
	
	/**
	 * get access token(password way)
	 * 
	 * @author tom.wang<tom.wang.sz@qq.com>
	 * @param string $params['username'] : the user(resource owner) name
	 * @param string $params['password'] : the user password
	 * @return string : access token 
	 */
	private function getAccessTokenByPassword($params) {
		$username = $params['username'];
		$password = $params['password'];
		$url = RenrenClient::getCfg('access_token_url');
		$params = array(
			'grant_type' => 'password',
			'username' => $username,
			'password' => $password,
			'client_id' => RenrenClient::getCfg('oauth_consumer_token'),
			'client_secret' => RenrenClient::getCfg('oauth_consumer_token_secret'),
		);
		$ret = RenrenUtil::http($url, 'POST', $params);
		$ret = json_decode($ret, true);
		
		return $ret;
	}
	
	/**
	 * get access token(refresh token way)
	 * http://wiki.dev.renren.com/wiki/%E4%BD%BF%E7%94%A8Refresh_Token
	 * 
	 * @author tom.wang<tom.wang.sz@qq.com>
	 * @param string $params['refresh_token'] : the refresh_token
	 * @return string : access token 
	 */
	private function getAccessTokenByRefresh($params) {
		$refresh_token = $params['refresh_token'];
		$url = RenrenClient::getCfg('access_token_url');
		$params = array(
			'grant_type' => 'refresh_token',
			'refresh_token' => $refresh_token,
			'client_id' => RenrenClient::getCfg('oauth_consumer_token'),
			'client_secret' => RenrenClient::getCfg('oauth_consumer_token_secret'),
		);
		$ret = RenrenUtil::http($url, 'POST', $params);
		$ret = json_decode($ret, true);
		
		return $ret;
	}
	
	/**
	 * handle error
	 */
	private function handleError($result) {
		if(isset($result['error']) && isset($result['error_description'])) {
			RenrenException::throwEx($result['error_description']);
		}	
	}
}
?>